package wechat.app.corp.domain

import java.util.Date
import org.apache.log4j.Logger
import wechat.app.WCCorpApplication
import wechat.app.WXCorpAppFactory
import wechat.base.snd.util.TimeFormatter
import org.eclipse.xtend.lib.annotations.Accessors

class EngineerNotificationCorpWeChat{
	static Logger logger = Logger::getLogger("EngineerNotificationCorpWeChat")
	private static String CALLBACK_SERVER_URL = "http://weixin.shrct.com:8090"  //"http://210.22.183.30:8090"
	
	@Accessors WCCorpApplication app
	@Accessors String cbkurl
	
	def static EngineerNotificationCorpWeChat getIns(){
		new	EngineerNotificationCorpWeChat
	}
//	def static void sendNormalNotification(Iterable<String> users, String title, String body, String id){
//		WXCorpAppFactory::customerApp=>[
//			createNews()=>[
//				withItem(title)=>[
////					picurl = CALLBACK_SERVER_URL+"/sample.jpg"
//					url = generateNotificationDetailsUrl(id)
//					logger.info('url========>'+url)
//				]
//				withItem(body)=>[
////					picurl = CALLBACK_SERVER_URL+"/smallsample.jpg"
//					url= generateNotificationDetailsUrl(id)
//				]
//				send2Users(users)
//			]
//		]
//	}
//	def static void sendEngineerTrainingNotification(Iterable<String> users, Date date, String location, String topic, String trainingID){
//		sendNormalNotification(newArrayList('rock20110501'), '培训通知','',CALLBACK_SERVER_URL)
//	}
	def  String generateNotificationDetailsUrl(String id){
		cbkurl+'/notificationdetail?notificationId='+id
	}
	def void sendNormalNotification(Iterable<String> users, String title, String body, String id){
		users.forEach[
			println('==========>send to '+ it)
		]
		if (users.size==0 || !users.exists[it!=null])  return;
		app.createNews()=>[
				withItem(title)=>[
//					picurl = CALLBACK_SERVER_URL+"/sample.jpg"
					url = generateNotificationDetailsUrl( id)
//					logger.info('url========>'+url)
				]
				withItem(body)=>[
//					picurl = CALLBACK_SERVER_URL+"/smallsample.jpg"
					url= generateNotificationDetailsUrl(id)
				]
				send2Users(users)
			]
	}
	
	def void sendNewAssignmentEvent (String wechatid, String assignmentId, String projectName, Date starttime, Date endtime) {
			val link = cbkurl + "/assignmentdetail?assignmentId=" + assignmentId + "&state=newtask"
			app.createNews()=>[
				withItem('您有任务未确认')=>[
//					picurl = CALLBACK_SERVER_URL+"/sample.jpg"
					url= link
				]
				withItem('项目名称：' + projectName + '\n任务开始时间：'+ TimeFormatter::timeFormat(starttime) + '\n任务结束时间：' +TimeFormatter::timeFormat(endtime))=>[
//					picurl = CALLBACK_SERVER_URL+"/smallsample.jpg"
					url= link
				]
				send2Users(newArrayList(wechatid))
			]
	}
	
	def void sendAssignmentAcceptEvent (String wechatid, String assignmentId, String projectName, String engineer, Date starttime, Date endtime) {
		
			app.createNews()=>[
				withItem('任务还未被接受')=>[
//					picurl = CALLBACK_SERVER_URL+"/sample.jpg"
//					url= "http://210.22.183.30:8090/assignmentdetail?assignmentId=" + assignmentId + "&state=newtask"
				]
				withItem('项目名称：' + projectName + '\n工程师：'+engineer + '\n任务开始时间：'+ TimeFormatter::timeFormat(starttime) + '\n任务结束时间：' + TimeFormatter::timeFormat(endtime))=>[
//					picurl = CALLBACK_SERVER_URL+"/smallsample.jpg"
//					url= generateUrl("","","11111", "测试任务", "测试任务描述")
				]
				send2Users(newArrayList(wechatid))
			]
	}
}